package org.groupg.practice;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedGraph;

import java.util.Set;

public class DAGIsolatedNodes {
    public static void main(String[] args) {
        // 创建一个有向图
        Graph<String, DefaultWeightedEdge> graph = new SimpleDirectedGraph<>(DefaultWeightedEdge.class);

        // 添加节点和边
        graph.addVertex("A");
        graph.addVertex("B");
        graph.addVertex("C");
        graph.addVertex("D");
        graph.addVertex("E"); // 添加一个孤立的节点
        graph.addEdge("A", "B");
        graph.addEdge("B", "C");
        graph.addEdge("B", "D");

        // 搜索没有被线连接的节点
        for (String vertex : graph.vertexSet()) {
            if (graph.inDegreeOf(vertex) == 0 && graph.outDegreeOf(vertex) == 0) {
                System.out.println("孤立节点: " + vertex);
            }
        }
    }
}